package de.gameduell.framework.queue {
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;	

	/**
	 * @author ddoe
	 */
	public class DelayAction extends EventDispatcher implements IGDAction {
		private var _timer:Timer;
		private var actionQueue:GDActionQueue;
		private var callbackFunction:Function;
		private var callbackParameter:Object;
		
		public function DelayAction(delay:Number,invokeNow:Boolean = false, callbackFunction:Function = null, callbackParameter:Object = null) {
			this.callbackFunction = callbackFunction;
			this.callbackParameter = callbackParameter;
			
			_timer = new Timer(delay);
			_timer.addEventListener(TimerEvent.TIMER, onTimerComplete);	
			actionQueue = GDActionQueue.getInstance();
			if(invokeNow){
				invoke();
			}		
		}
			
		private function onTimerComplete(event:TimerEvent):void{
			_timer.reset();
			_timer.stop();
			dispatchEvent(new ActionEvent(ActionEvent.DONE));
			if (callbackFunction != null) {
				callbackFunction(callbackParameter);
			}
		}
		
		public function compute():void {
			_timer.start();
		}
		
		public function invoke():void {
			actionQueue.add(this);
		}
		
		public function get description():String {
			return "DelayAction delay = "+_timer.delay;
		}
	}
}
